// 测试 prevPay 函数功能
// 这个文件用于测试支付预览功能是否正常工作

console.log('=== 支付预览功能测试 ===');

// 模拟页面数据结构
const mockPageData = {
    // 支付相关
    bizId: 'test_biz_123',
    payPrice: '100.00',
    payMoney: '70.00', // 折扣后金额
    originalPayMoney: '100.00',
    useScore: 20, // 云豆使用量
    useCardScore: 30, // 福利卡使用量
    remainMoney: '20.00', // 微信支付金额
    discount: 70, // 70%折扣
    discount_txt: '7折优惠',
    hasDiscount: true,
    
    // 卡片相关
    selectedCards: [
        { id: 1, card_name: '福利卡A', card_score: 30, score: 30 },
        { id: 2, card_name: '福利卡B', card_score: 20, score: 20 }
    ],
    
    // 用户信息
    userScore: 50
};

// 模拟 prePay 函数
function prePay() {
    console.log('=== 执行 prePay 函数 ===');
    
    // 检查支付金额
    const payMoneyNum = parseFloat(mockPageData.payMoney) || 0;
    if (payMoneyNum <= 0) {
        console.log('❌ 错误：请输入有效的支付金额');
        return;
    }

    // 检查商家ID
    if (!mockPageData.bizId) {
        console.log('❌ 错误：商家信息错误');
        return;
    }

    // 重新计算支付方案（模拟）
    calculatePayPlan();

    // 显示支付预览面板
    console.log('✅ 显示支付预览面板');
    
    console.log('支付预览信息:', {
        payMoney: mockPageData.payMoney,
        useScore: mockPageData.useScore,
        useCardScore: mockPageData.useCardScore,
        remainMoney: mockPageData.remainMoney,
        hasDiscount: mockPageData.hasDiscount,
        discount: mockPageData.discount,
        originalPayMoney: mockPageData.originalPayMoney
    });
}

// 模拟计算支付方案函数
function calculatePayPlan() {
    console.log('🔄 重新计算支付方案...');
    
    const { payMoney, userScore, selectedCards } = mockPageData;
    const payMoneyNum = parseFloat(payMoney) || 0;
    
    if (payMoneyNum <= 0) return;
    
    let totalUseScore = 0;
    let totalCardScore = 0;
    let useScore = 0;
    
    // 1. 计算用户选择的卡片云豆使用量
    if (selectedCards.length > 0) {
        let remainingPayMoney = payMoneyNum;
        
        // 按卡片云豆数量从高到低排序
        const sortedCards = [...selectedCards].sort((a, b) => {
            const scoreA = a.card_score || a.score || 0;
            const scoreB = b.card_score || b.score || 0;
            return scoreB - scoreA;
        });
        
        for (const card of sortedCards) {
            const cardScore = card.card_score || card.score || 0;
            if (cardScore > 0 && remainingPayMoney > 0) {
                const useCardScore = Math.min(cardScore, remainingPayMoney);
                totalCardScore += useCardScore;
                totalUseScore += useCardScore;
                remainingPayMoney -= useCardScore;
            }
        }
    }
    
    // 2. 计算云豆使用量
    const remainingMoney = payMoneyNum - totalUseScore;
    if (remainingMoney > 0) {
        const needScore = remainingMoney;
        useScore = Math.min(userScore, needScore);
        totalUseScore += useScore;
    }
    
    // 3. 计算微信支付补差价
    const remainMoney = (payMoneyNum - totalUseScore).toFixed(2);
    
    console.log('支付方案计算结果:', {
        totalUseScore,
        totalCardScore,
        useScore,
        remainMoney
    });
}

// 模拟 doWxPay 函数
function doWxPay() {
    console.log('=== 执行 doWxPay 函数 ===');
    console.log('✅ 关闭预览面板');
    console.log('🔄 调用真实的支付请求...');
    
    // 这里会调用 startPay() 函数
    startPay();
}

// 模拟 startPay 函数
function startPay() {
    console.log('=== 执行 startPay 函数 ===');
    console.log('🔄 开始真实支付流程...');
    
    // 模拟支付API调用
    setTimeout(() => {
        console.log('✅ 支付请求成功');
        console.log('📱 如果需要微信支付，将调用微信支付接口');
    }, 1000);
}

// 执行测试
console.log('\n--- 测试开始 ---');
prePay();

console.log('\n--- 测试支付确认 ---');
setTimeout(() => {
    doWxPay();
}, 2000);

console.log('\n=== 测试完成 ===');
